package UnitTest.DataStructureTest.arrayANDlistTest.listTest;

import DataStructure.arrayANDlist.list.Listlj;
import DataStructure.arrayANDlist.list.listRealize.SinglyLinkedList;
import DataStructure.arrayANDlist.list.listRealize.TheLastK;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author liujun
 * @version 1.0
 * @date 2021/1/6
 * @author—Email liujunfirst@outlook.com
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 输出链表的倒数第K个元素 测试案例
 */
public class TheLastKTest {

    TheLastK tk = new TheLastK();
    Listlj listlj = new SinglyLinkedList();

    @Test
    public void testgetlastk() throws Exception {
        test("getlastk");
    }

    @Test
    public void testgetlastk1() throws Exception {
        test("getlastk1");
    }

    @Test
    public void testgetlastk_recursion() throws Exception {
        test("getlastk_recursion");
    }


    private void test(String method0) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Class clz = Class.forName("DataStructure.arrayANDlist.list.listRealize.TheLastK");
        //反射获取方法，需要注意属性：int[].class
        Method method = clz.getMethod(method0, Listlj.class, int.class);
        Constructor appleConstructor = clz.getConstructor();
        Object TheLastK = appleConstructor.newInstance();

        listlj.insert(false, demo01);
        int answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target01;


        listlj.clear();
        listlj.insert(false, demo02);
        answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target02;

        listlj.clear();
        listlj.insert(false, demo03);
        answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target03;

        listlj.clear();
        listlj.insert(false, demo04);
        answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target04;

        listlj.clear();
        listlj.insert(false, demo05);
        answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target05;

        listlj.clear();
        listlj.insert(false, demo06);
        answer = (int) method.invoke(TheLastK, listlj, 5);
        assert answer == target06;
    }

    int[] demo01 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    int target01 = 6;
    int[] demo02 = {7, 3, 8, 5, 9, 7, 6, 5, 9};
    int target02 = 9;
    int[] demo03 = {};
    int target03 = Integer.MIN_VALUE;
    int[] demo04 = null;
    int target04 = Integer.MIN_VALUE;
    int[] demo05 = {9, 8, 7, 6, 5};
    int target05 = 9;
    int[] demo06 = {0, 96, 5, 43, 456, 78, 909, 87, 65, 4, 3, 4, 5, 7, 8, 9, 0, 9, 8, 765, 43, 456, 7898, 765, 90};
    int target06 = 43;
}
